import { createBrowserRouter, RouteObject } from 'react-router-dom'
import Login from '../views/login/login'
import Welcome from '../views/welcome/index'
import Error404 from '../views/404'
import Error403 from '../views/403'
import { Home } from '../views/home'
import { Navigate } from 'react-router-dom'
import Layout from '../layout/index'
import AuthLoader from './authLoader'
import { lazyLoad } from './lazyLoad'
import React from 'react'
// 定义一个包含 meta 属性的自定义路由类型,并在children中递归定义该类型
type CustomRouteObject = RouteObject & {
  meta?: {
    auth: boolean
  }
  children?: CustomRouteObject[]
}
const router: CustomRouteObject[] = [
  {
    path: '/',
    element: <Navigate to={'/login'} />
  },
  {
    id: 'layout',
    element: <Layout />,
    loader: AuthLoader,
    children: [
      {
        path: '/welcome',
        element: <Welcome />
      },
      {
        path: '/dashboard',
        element: lazyLoad(React.lazy(() => import('../views/dashboard')))
      },
      {
        path: '/userList',
        element: lazyLoad(React.lazy(() => import('../views/system/user')))
      },
      {
        path: '/deptList',
        element: lazyLoad(React.lazy(() => import('../views/system/dept')))
      },
      {
        path: '/menuList',
        element: lazyLoad(React.lazy(() => import('../views/system/menu'))),
        meta: {
          auth: false
        }
      },
      {
        path: '/roleList',
        element: lazyLoad(React.lazy(() => import('../views/system/role')))
      }
    ]
  },
  {
    path: '/login',
    element: <Login />
  },
  {
    path: '*',
    element: <Navigate to='/404' />
  },
  {
    path: '/404',
    element: <Error404 />
  },
  {
    path: '/403',
    element: <Error403 />
  },
  {
    path: '/home',
    element: <Home />
  }
]

// export { router }
const browserRouter = createBrowserRouter(router)

export { browserRouter as router }
